package com.shoufeng.demo.client;

import com.shoufeng.demo.grpc.HealthCheckRequest;
import com.shoufeng.demo.grpc.HealthCheckResponse;
import com.shoufeng.demo.grpc.HealthGrpc;
import com.shoufeng.demo.grpc.HealthGrpc.HealthBlockingStub;
import io.grpc.ManagedChannel;
import io.grpc.netty.NettyChannelBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class HealthClient {

  private static final Logger LOGGER = LoggerFactory.getLogger(HealthClient.class);

  @Value("${health.server.url}")
  private String url;

  @Value("${health.server.port}")
  private Integer port;

  public void checkServer(){
    String url = "127.0.0.1";
    int port = 8090;
    ManagedChannel managedChannel = NettyChannelBuilder.forAddress(url, port).usePlaintext(true).build();
    HealthBlockingStub healthBlockingStub = HealthGrpc
        .newBlockingStub(managedChannel);
    HealthCheckResponse healthCheckResponse = healthBlockingStub
        .check(HealthCheckRequest.newBuilder().setService("AAAAAAAAAAAA").build());
    LOGGER.info("请求服务端返回: " + healthCheckResponse.getStatus());
  }

  public static void main(String[] args) {
    HealthClient healthClient = new HealthClient();
    healthClient.checkServer();
  }
}
